//
//  MyOrderViewController.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderViewController.h"
#import "BKMyOrderTitleView.h"
#import "BKMyCommon.h"
#import "BKMyOrderMainCell.h"
#import "CellModel.h"

#define Height_TitleView   36

@interface BKMyOrderViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,BK_OrderTitle_delegate>

@property(nonatomic,strong)BKMyOrderTitleView *TitleView;
@property(nonatomic,strong)NSArray *arr_Cell;


@end

@implementation BKMyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  setProperty];
    [self  createUI];
}
#pragma mark==================setProperty======================
-(void)setProperty{
    self.view .backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    /*
     * 构建模型数组
     */
    [self BK_Order_Create_Cell_Arr];
    
}

#pragma mark==================createUI======================
-(void)createUI{
    [self BK_Create_Colletion_View];
    [self BK_Create_OrderTitle_FormStr:_str_type];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)BK_Order_Create_Cell_Arr{
    
    CellModel *model0 = [[CellModel alloc]init];
    model0.className = NSStringFromClass([BKMyOrderMainCell class]);
    model0.reuseIdentifier = @"BKMyOrder_00";
    model0.title  = @"";
    
    CellModel *model1 = [[CellModel alloc]init];
    model1.className = NSStringFromClass([BKMyOrderMainCell class]);
    model1.reuseIdentifier = @"BKMyOrder_01";
    model1.title = @"01";
    
    CellModel *model2 = [[CellModel alloc]init];
    model2.className = NSStringFromClass([BKMyOrderMainCell class]);
    model2.reuseIdentifier = @"BKMyOrder_02";
    model2.title = @"02";
    
    CellModel *model3 = [[CellModel alloc]init];
    model3.className = NSStringFromClass([BKMyOrderMainCell class]);
    model3.reuseIdentifier = @"BKMyOrder_03";
    model3.title = @"03";
    CellModel *model4 = [[CellModel alloc]init];
    model4.className = NSStringFromClass([BKMyOrderMainCell class]);
    model4.reuseIdentifier = @"BKMyOrder_401";
    model4.title  = @"401";
    
    self.arr_Cell = [NSArray arrayWithObjects:model0,model1,model2,model3,model4, nil];
    
}


/*
 * 创建CollectionView
 */
-(void)BK_Create_Colletion_View{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(ScreenWidth, self.view.frame.size.height-64-Height_TitleView);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0.0f;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, Height_TitleView, ScreenWidth, self.view.frame.size.height-Height_TitleView-64) collectionViewLayout:layout];
    
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    
    for (CellModel* model in self.arr_Cell) {
        [_collectionView registerClass:NSClassFromString(model.className) forCellWithReuseIdentifier:model.reuseIdentifier];
    }
}


/*
 * 创建OrderTitle
 */
-(void)BK_Create_OrderTitle_FormStr:(NSString*)str{
    
    self.TitleView= [[BKMyOrderTitleView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, Height_TitleView)];
    [self.view addSubview:self.TitleView];
    self.TitleView.delegate = self;
    self.TitleView.curPage = [str integerValue];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arr_Cell.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellModel *model = self.arr_Cell[indexPath.row];
    BKMyOrderMainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:model.reuseIdentifier forIndexPath:indexPath];
    cell.str_number = model.title;
    cell .backgroundColor = [UIColor redColor];
    if (!cell) {
        cell = [[NSClassFromString(model.className)alloc]init];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}

//结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[UICollectionView class]]){
        NSInteger row=(scrollView.contentOffset.x+SCREENWIDTH/2.0)/SCREENWIDTH;
        self.TitleView.curPage = row;
        
    }
}
/*
 * 点击Title的代理方法
 */
-(void)BK_Order_Delegate_TitleButton_Click:(UIButton *)btn{
    [self BK_set_OrderNav_From:btn.titleLabel.text];
    NSIndexPath *path  = [NSIndexPath indexPathForRow:btn.tag inSection:0];
    [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:  UICollectionViewScrollPositionLeft  animated:YES];
    
}

/*
 * 设置title
 */
-(void)BK_set_OrderNav_From:(NSString*)str{
    self.title = str;
    if ([str isEqualToString:@"全部"]) {
        self.title = @"全部订单";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end






















